package com.tengju.bff.interfaces.manage.star;

import com.tengju.bff.interfaces.app.user.assembler.StarSignInfoAssembler;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.archives.dto.BatchIdCodesDTO;
import com.tengju.user.application.archives.dto.BatchStarLimitDTO;
import com.tengju.user.application.archives.dto.StarLimitInfoDTO;
import com.tengju.user.domain.model.sign.StarLimitInfo;
import com.tengju.user.domain.model.sign.StarLimitRepository;
import com.tengju.user.domain.model.user.ManageUser;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@Api(tags = "腾炬黑名单白名单管理")
@RequestMapping(value = "manage/namelist")
@Slf4j
@ManageEntrance
public class NameListController {

    @Autowired
    StarLimitRepository starLimitRepository;

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台批量添加白名单", notes = "DT后台批量添加白名单")
    @RequestMapping(value = "batchAddWhiteUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchAddWhiteUser(@RequestBody BatchIdCodesDTO batchIdCodesDTO, BindingResult bindingResult) {
        Staff staff = ManageUtil.getOperateUser();
        starLimitRepository.batchAddWhiteUser(
                Optional.ofNullable(batchIdCodesDTO.getIdCodes())
                        .map(list -> list.stream()
                                .filter(Objects::nonNull)
                                .map(UserIdCode::new)
                                .collect(Collectors.toList())
                        ).orElse(null),
                staff.getName());
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台批量删除白名单", notes = "DT后台批量删除白名单")
    @RequestMapping(value = "batchDeleteWhiteUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchDeleteWhiteUser(@RequestBody BatchIdCodesDTO batchIdCodesDTO, BindingResult bindingResult) {
        Staff staff = ManageUtil.getOperateUser();
        starLimitRepository.batchDeleteWhiteUser(
                Optional.ofNullable(batchIdCodesDTO.getIdCodes())
                        .map(list -> list.stream()
                                .filter(Objects::nonNull)
                                .map(UserIdCode::new)
                                .collect(Collectors.toList())
                        ).orElse(null),
                staff.getName());
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台批量更新黑名单", notes = "DT后台批量更新黑名单")
    @RequestMapping(value = "batchUpdateStarLimit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchUpdateStarLimit(@RequestBody BatchStarLimitDTO batchStarLimitDTO, BindingResult bindingResult) {
        Staff staff = ManageUtil.getOperateUser();
        List<StarLimitInfoDTO> starLimitInfoDTOList = batchStarLimitDTO.getStarLimitInfoList();
        if (CollectionUtils.isEmpty(starLimitInfoDTOList)) {
            return ApiResponse.newSuccess(true);
        }
        List<StarLimitInfo> starLimitInfoList = starLimitInfoDTOList.stream().map(StarSignInfoAssembler::limitDTOToEntity)
                .collect(Collectors.toList());
        starLimitRepository.batchUpdateStarLimit(starLimitInfoList, staff);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "DT后台批量删除黑名单", notes = "DT后台批量更新黑名单")
    @RequestMapping(value = "batchDeleteBlackUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> batchDeleteBlackUser(@RequestBody BatchIdCodesDTO batchIdCodesDTO, BindingResult bindingResult) {
        Staff staff = ManageUtil.getOperateUser();
        starLimitRepository.batchDeleteBlackUser(
                Optional.ofNullable(batchIdCodesDTO.getIdCodes())
                        .map(list -> list.stream()
                                .filter(Objects::nonNull)
                                .map(UserIdCode::new)
                                .collect(Collectors.toList())
                        ).orElse(null),
                staff.getName());
        return ApiResponse.newSuccess(true);
    }

}
